//////////////////////////////////////////ok
#include"stdafx.h"
#include "bochs.h"




void IA32_CPU::MUL_EAXEd(Ia32_Instruction_c *i)
{
  Bit32u op1_32, op2_32, product_32h, product_32l;
  Bit64u product_64;

  op1_32 = EAX;
  if (i->modC0()) 
  {
    op2_32 = IA32_READ_32BIT_REG(i->rm());
  }
  else 
  {
    read_virtual_dword(i->seg(), IA32_RMAddr(i), &op2_32);
  }

  product_64  = ((Bit64u) op1_32) * ((Bit64u) op2_32);
  product_32l = (Bit32u) (product_64 & 0xFFFFFFFF);
  product_32h = (Bit32u) (product_64 >> 32);
  IA32_SET_FLAGS_OSZAPC_S1S2_32(product_32l, product_32h, IA32_INSTR_MUL32);
  EAX = product_32l;
  EDX = product_32h;
}

void IA32_CPU::IMUL_EAXEd(Ia32_Instruction_c *i)
{
  Bit32s op1_32, op2_32;

  op1_32 = EAX;
  if (i->modC0()) 
  {
    op2_32 = IA32_READ_32BIT_REG(i->rm());
  }
  else 
  {
    read_virtual_dword(i->seg(), IA32_RMAddr(i), (Bit32u *) &op2_32);
  }

  Bit64s product_64  = ((Bit64s) op1_32) * ((Bit64s) op2_32);
  Bit32u product_32l = (Bit32u) (product_64 & 0xFFFFFFFF);
  Bit32u product_32h = (Bit32u) (product_64 >> 32);
  EAX = product_32l;
  EDX = product_32h;
  IA32_SET_FLAGS_OSZAPC_S1S2_32(product_32l, product_32h, IA32_INSTR_IMUL32);
}

void IA32_CPU::DIV_EAXEd(Ia32_Instruction_c *i)
{
  Bit32u op2_32, remainder_32, quotient_32l;
  Bit64u op1_64, quotient_64;

  op1_64 = (((Bit64u) EDX) << 32) + ((Bit64u) EAX);

  if (i->modC0()) 
  {
    op2_32 = IA32_READ_32BIT_REG(i->rm());
  }
  else 
  {
    read_virtual_dword(i->seg(), IA32_RMAddr(i), &op2_32);
  }

  if (op2_32 == 0) 
  {
    exception(IA32_BX_DE_EXCEPTION, 0, 0);
  }

  quotient_64  = op1_64 / op2_32;
  remainder_32 = (Bit32u) (op1_64 % op2_32);
  quotient_32l = (Bit32u) (quotient_64 & 0xFFFFFFFF);
  if (quotient_64 != quotient_32l)
  {
    exception(IA32_BX_DE_EXCEPTION, 0, 0);
  }
  EAX = quotient_32l;
  EDX = remainder_32;

}

void IA32_CPU::IDIV_EAXEd(Ia32_Instruction_c *i)
{
  Bit32s op2_32, remainder_32, quotient_32l;
  Bit64s op1_64, quotient_64;

  op1_64 = (((Bit64u) EDX) << 32) | ((Bit64u) EAX);

  if (i->modC0()) 
  {
    op2_32 = IA32_READ_32BIT_REG(i->rm());
  }
  else 
  {
    read_virtual_dword(i->seg(), IA32_RMAddr(i), (Bit32u *) &op2_32);
  }

  if (op2_32 == 0)
    exception(IA32_BX_DE_EXCEPTION, 0, 0);

  if ((op1_64 == ((Bit64s)IA32_CONST64(0x8000000000000000LL))) && (op2_32 == -1))
    exception(IA32_BX_DE_EXCEPTION, 0, 0);

  quotient_64  = op1_64 / op2_32;
  remainder_32 = (Bit32s) (op1_64 % op2_32);
  quotient_32l = (Bit32s) (quotient_64 & 0xFFFFFFFF);
  if (quotient_64 != quotient_32l)
  {
    exception(IA32_BX_DE_EXCEPTION, 0, 0);
  }

  EAX = quotient_32l;
  EDX = remainder_32;

}

void IA32_CPU::IMUL_GdEdId(Ia32_Instruction_c *i)
{
  Bit32s op2_32, op3_32;

  op3_32 = i->Id();
  if (i->modC0()) 
  {
    op2_32 = IA32_READ_32BIT_REG(i->rm());
  }
  else 
  {
    read_virtual_dword(i->seg(), IA32_RMAddr(i), (Bit32u *) &op2_32);
  }

  Bit64s product_64  = ((Bit64s) op2_32) * ((Bit64s) op3_32);
  Bit32u product_32l = (Bit32u)(product_64 & 0xFFFFFFFF);
  Bit32u product_32h = (Bit32u)(product_64 >> 32);
  IA32_WRITE_32BIT_REGZ(i->nnn(), product_32l);
  IA32_SET_FLAGS_OSZAPC_S1S2_32(product_32l, product_32h, IA32_INSTR_IMUL32);

}

void IA32_CPU::IMUL_GdEd(Ia32_Instruction_c *i)
{
  Bit32s op1_32, op2_32;

  if (i->modC0()) 
  {
    op2_32 = IA32_READ_32BIT_REG(i->rm());
  }
  else 
  {
    read_virtual_dword(i->seg(), IA32_RMAddr(i), (Bit32u *) &op2_32);
  }

  op1_32 = IA32_READ_32BIT_REG(i->nnn());
  Bit64s product_64 = ((Bit64s) op1_32) * ((Bit64s) op2_32);
  Bit32u product_32l = (Bit32u)(product_64 & 0xFFFFFFFF);
  Bit32u product_32h = (Bit32u)(product_64 >> 32);
  IA32_WRITE_32BIT_REGZ(i->nnn(), product_32l);
  IA32_SET_FLAGS_OSZAPC_S1S2_32(product_32l, product_32h, IA32_INSTR_IMUL32);
}
